package com.mazaiting.uac.utils;

import com.mazaiting.uac.comps.FixedArithmeticCaptcha;
import com.mazaiting.uac.constant.enums.CaptchaTypeEnum;
import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.ChineseGifCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;

import java.awt.*;

/**
 * 验证码生产者
 */
public class CaptchaUtil {

    /**
     * 获取图片验证码
     * @param type 验证码类型
     * @return 图形验证码
     */
    public static Captcha getCaptcha(CaptchaTypeEnum type) {
        Captcha captcha;
        // 验证码高度
        int height = 36;
        // 验证码内容长度
        int length = 4;
        // 验证码宽度
        int width = 120;
        switch (type) {
            case ARITHMETIC -> {
                captcha = new FixedArithmeticCaptcha(width, height);  // 算术类型
                captcha.setLen(2); //固定设置为两位，图片为算数运算表达式
            }
            case CHINESE -> {
                captcha = new ChineseCaptcha(width, height);
                captcha.setLen(length);
            }
            case CHINESE_GIF -> {
                captcha = new ChineseGifCaptcha(width, height);
                captcha.setLen(length);
            }
            case GIF -> {
                captcha = new GifCaptcha(width, height);//最后一位是位数
                captcha.setLen(length);
            }
            case SPEC -> {
                captcha = new SpecCaptcha(width, height);
                captcha.setLen(length);
            }
            default -> throw new RuntimeException("验证码配置信息错误！");
        }
        // 验证码字体
        String fontName = "Verdana";
        // 字体风格
        int fontStyle = Font.PLAIN;
        // 字体大小
        int fontSize = 20;
        captcha.setFont(new Font(fontName, fontStyle, fontSize));
        return captcha;
    }



}
